<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '新增织物名称' : '编辑织物名称'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="1000px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="12">
            <el-form-item label="织物名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入织物名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="织物类型：" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择织物类型" clearable>
                <el-option v-for="item in typeArray" :key="item.key" :label="item.value" :value="item.key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示排序：" prop="reorder">
              <el-input v-model="ruleForm.reorder" placeholder="请输入显示排序" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="尺码：" prop="size">
              <el-select v-model="ruleForm.size" placeholder="请选择尺码" clearable filterable multiple>
                <el-option v-for="item in sizeArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="洗涤单价：" prop="price">
              <el-input v-model="ruleForm.price" placeholder="请输入洗涤单价" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { clothClassifyInfo, clothClassifyAdd, clothClassifyEdit } from "@/api/clothes";
import { getClothSize, getClothType } from "@/api/system";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        name: "",
        type: "",
        reorder: "",
        size:[],
        price: "",
      },
      rules: {
        // 表单规则
        name: [{ required: true, message: "请输入织物名称", trigger: "blur" }],
        type: [{ required: true, message: "请选择织物类型", trigger: "change" }],
        reorder: [{ required: true, message: "请输入显示排序", trigger: "blur" }],
        size: [{ required: true, message: "请选择尺码", trigger: "change" }],
        price: [{ required: false, message: "请输入价格", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      typeArray: [],
      sizeArray: [],
      activeId: '',
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

      // 获取尺码集合
      this.getClothSizeList();

      this.getClothTypeList();
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      clothClassifyInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.id == "") {
          clothClassifyAdd(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
          } else {
          clothClassifyEdit(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
        }
      });
    },
    // 获取尺码集合
    getClothSizeList(){
      getClothSize({}).then((res) => {
        if (res.code == 200) {
          this.sizeArray = res.data;
        }
      });
    },
    // 获取织物类型列表
    getClothTypeList() {
      getClothType().then((res) => {
        if (res.code == 200) {
          this.typeArray = res.data;
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
.main {
  width: 100%;
  height: 100%;
  display: flex;
}
.card-item {
  width: 100%;
  height: 40px;
  line-height: 40px;
  cursor: pointer;
  border-bottom: solid 1px #f2f2f2;
  display: flex;
  justify-content: space-between;
  padding: 0px 10px;
}
// .card-item:hover{
//   background-color: #e8e8e8;
// }
.active {
  background-color: #d5e5e4;
}
.twoClass {
  display: flex;
  align-items: center;
  border: 1px solid #ebeef5;
}
.oneBox {
  // margin-left: 30px;
  // height: 15.62rem;
  // overflow: auto;
}
.twoBox {
  margin-left: 50px;
  display: flex;
  flex-direction: column;
  padding: 10px 10px;
}
.threeClass {
  display: flex;
  align-items: center;
  border: 1px solid #ebeef5;
}
.fourClass {
  display: flex;
  align-items: center;
  margin-top: 5px;
  // border: 1px solid #ebeef5;
}
.twocheckbox {
  width: 100px;
  display: flex;
  flex-direction: column;
  margin-left: 20px;
}
.threeBox {
  margin-left: 60px;
  margin-right: 30px;
  margin-bottom: 5px;
}
.trecheckbox {
  margin-left: 20px;
  margin-right: 20px;
  // display: flex;
  // flex-direction: column;
}
.box-card1 {
  width: 300px;
  height: 400px;
  margin-left: 90px;
}
</style>
